package com.ebeijia.web.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ebeijia.dto.OrderInfoDto;
import com.ebeijia.exception.ServiceException;
import com.ebeijia.service.user.UserService;
import com.ebeijia.util.XmlUtils;
import com.ebeijia.web.ResponseMessage;

@RestController
@RequestMapping("/pay")
public class PayHandleController {
	@Autowired
	private UserService userService;
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@RequestMapping(value = "/addOrder", method = RequestMethod.POST)
	public ResponseMessage addOrder(OrderInfoDto orderDto)
			throws ServiceException {
		Pattern patterAmt = Pattern.compile("^[0-9]+(.[0-9]{1,2})?$",
				Pattern.CASE_INSENSITIVE);
		if (StringUtils.isEmpty(orderDto.getOrderAmt())) {
			return ResponseMessage.error("金额不可为空");
		}
		Matcher m = patterAmt.matcher(orderDto.getOrderAmt());
		if (!m.matches()) {
			return ResponseMessage.error("金额格式不正确");
		}

		return userService.addOrderWithPay(orderDto);

	}

	@RequestMapping(value = "/notice")
	public String noticePayResult(HttpServletRequest req) throws Exception {

		String respString = "error";
		try {
			req.setCharacterEncoding("utf-8");
			String resString = XmlUtils.parseRequst(req);
			logger.info("======================支付通知请求接口数据==================："
					+ resString);
			Map<String, String> map = null;
			if (StringUtils.isNotEmpty(resString)) {
				map = XmlUtils.toMap(resString.getBytes("utf-8"), "utf-8");
				String result = userService.payResultHandle(map);
				if ("0".equals(result)) {
					respString = "success";
				}
			} else {
				respString = "fail";
			}
		} catch (Exception e) {
			respString = "fail";
			logger.error(e.getMessage(), e);
		}
		return respString;
	}
	@RequestMapping(value = "/notice2")
	public String noticePayResult2(HttpServletRequest req) throws Exception {

		String respString = "error";
		try {
			req.setCharacterEncoding("utf-8");
			String resString ="<xml><attach><![CDATA[任丘水务充值]]></attach><bank_type><![CDATA[CFT]]></bank_type><charset><![CDATA[UTF-8]]></charset><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[N]]></is_subscribe><mch_id><![CDATA[7551000001]]></mch_id><nonce_str><![CDATA[1466677576371]]></nonce_str><openid><![CDATA[oywgtuN51YxzU93zAS6q4kpwLrbA]]></openid><out_trade_no><![CDATA[201606230000000641]]></out_trade_no><out_transaction_id><![CDATA[4003042001201606237761491107]]></out_transaction_id><pay_result><![CDATA[0]]></pay_result><result_code><![CDATA[0]]></result_code><sign><![CDATA[83AFCC248C087ACF81B631C4D3173323]]></sign><sign_type><![CDATA[MD5]]></sign_type><status><![CDATA[0]]></status><time_end><![CDATA[20160623182615]]></time_end><total_fee><![CDATA[1]]></total_fee><trade_type><![CDATA[pay.weixin.jspay]]></trade_type><transaction_id><![CDATA[7551000001201606234468686062]]></transaction_id><version><![CDATA[2.0]]></version></xml>";
			logger.info("======================支付通知请求接口数据==================："
					+ resString);
			Map<String, String> map = null;
			if (StringUtils.isNotEmpty(resString)) {
				map = XmlUtils.toMap(resString.getBytes("utf-8"), "utf-8");
				String result = userService.payResultHandle(map);
				if ("0".equals(result)) {
					respString = "success";
				}
			} else {
				respString = "fail";
			}
		} catch (Exception e) {
			respString = "fail";
			logger.error(e.getMessage(), e);
		}
		return respString;
	}

}
